﻿using System.Net.Sockets;
using mqtt.client.test.Mqtt;
using XjjXmm.Infrastructure.Common;

namespace mqtt.client.test.Output;

internal class ReceiveResponse
{
    private readonly ArraySegment<byte> buffer;
    private readonly Socket socket;
   // private readonly byte qos;

    public ReceiveResponse(ArraySegment<byte> buffer, Socket socket, byte qos)
    {
        this.buffer = buffer;
        this.socket = socket;
      //  this.qos = qos;
    }

    public async Task< Response<string>> Result()
    {
        var receiveData = new List<byte>();

        var qos = (byte)(buffer[0] & 0x06) >> 1;

        var bytes = buffer[1..];

        int len = 0;

        int packetIdentifier = 0;

        while (true)
        {
            if (receiveData.Count == 0)
            {
                len = Util.ComputeRemainingLength(bytes);
                if (len < 128)
                {
                    bytes = bytes[1..];
                }
                else if (len < 16384)
                {
                    bytes = bytes[2..];
                }
                else
                {
                    bytes = bytes[3..];
                }

                var subjectLength = (bytes[0] << 8) | bytes[1];
                len = len - subjectLength - 2;

                bytes = bytes[(subjectLength + 2)..];

                if (qos > 0)
                {
                    len -= 2;
                    packetIdentifier = bytes[0] << 8 | bytes[1];

                    bytes = bytes[2..];
                }


                if (len < bytes.Count)
                {
                    bytes = bytes[0..len];
                }  
            }


            receiveData.AddRange(bytes.ToArray());

            if (receiveData.Count == len)
            {
                Console.WriteLine(receiveData.ToStrings());
                Console.WriteLine();

                //  string test = "乐山大佛你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗结尾乐山大佛你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗结尾乐山大佛你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗你好吗结尾";
                // var testByte = GetBytes(test);

                //receiveData.Clear();

                if (qos == Qos.Qos1)
                {
                 //   await SendPubAck(packetIdentifier);
                }
                else if (qos == Qos.Qos2)
                {
                  //  await SendPubRec(packetIdentifier);

                    var count = await socket.ReceiveAsync(buffer, SocketFlags.None);
                    bytes = buffer[0..count];

                    var res = new PublishRelOutput(bytes);
                  //  await SendPubComp(res.Result().Result);
                }

                return new Response<string>( receiveData.ToStrings());
            }
            else
            {
                var count = await socket.ReceiveAsync(buffer, SocketFlags.None);
                bytes = buffer[0..count];
            }
        }

        
    }

    /*private async Task SendPubAck(int packetIdentifier)
    {
        var packet = new PublishAckDataPacket(packetIdentifier);

        await socket.SendAsync(packet.ToBytes());
    }

    private async Task SendPubRec(int packetIdentifier)
    {
        var packet = new PublishRecDataPacket(packetIdentifier);

        await socket.SendAsync(packet.ToBytes());

       
    }

    private async Task SendPubComp(int packetIdentifier)
    {
        var packet = new PublishCompDataPacket(packetIdentifier);

        await socket.SendAsync(packet.ToBytes());


    }*/
}
